//
//  ABProgressView.m
//  ABProgressView
//
//  Created by apple on 16/4/18.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ABProgressView.h"
@interface ABProgressView()
@property (nonatomic, strong)UILabel *buttomLabel;
@property (nonatomic, strong)UILabel *overLabel;
@property (nonatomic, strong)UILabel *endLabel;
@property (nonatomic, strong)UIButton *selectBut;
//@property (nonatomic, strong)UIButton *endBut;
@property (nonatomic, assign)CGFloat percent;
@end

@implementation ABProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.buttomLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, self.frame.size.width, frame.size.height - 20)];
        //**************底层Label颜色************
        self.buttomLabel.backgroundColor = [UIColor colorWithRed:195/255.0 green:196/255.0 blue:197/255.0 alpha:1];
        self.buttomLabel.text = @"拖动滑块,完成验证";
        self.buttomLabel.textAlignment = NSTextAlignmentCenter;
        self.buttomLabel.textColor = [UIColor colorWithRed:156/255.0 green:157/255.0 blue:198/255.0 alpha:1];
        self.buttomLabel.layer.masksToBounds = 5;
        self.buttomLabel.layer.cornerRadius = 15;
        
        self.overLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, 0, frame.size.height - 20)];
        self.overLabel.layer.masksToBounds = 5;
        self.overLabel.layer.cornerRadius = 15;
        //**************上层Label颜色************
//        self.overLabel.backgroundColor = [UIColor colorWithRed:230/255.0 green:71/255.0 blue:59/255.0 alpha:1];
        self.overLabel.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"SHOUYE1_32"]];

        //        self.endLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.frame.size.width, self.frame.size.height/2, 0, 1)];
//        //**************右侧Label颜色************
//        self.endLabel.backgroundColor = [UIColor redColor];
        
        self.selectBut = [[UIButton alloc]initWithFrame:CGRectMake(5, 0, self.frame.size.height, self.frame.size.height)];
        self.selectBut.center = CGPointMake(0, self.frame.size.height/2);
        [self.selectBut setBackgroundImage:[UIImage imageNamed:@"progressleft"] forState:UIControlStateNormal];
        
//        self.endBut = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.height, self.frame.size.height)];
//        self.endBut.center = CGPointMake(self.frame.size.width, self.frame.size.height/2);
        
        //****************按钮颜色***************
//        self.selectBut.backgroundColor = [UIColor yellowColor];
//        self.endBut.backgroundColor = [UIColor cyanColor];
        
        self.selectBut.layer.masksToBounds = YES;
        self.selectBut.layer.cornerRadius = self.frame.size.height/2;
//        self.endBut.layer.masksToBounds = YES;
//        self.endBut.layer.cornerRadius = self.frame.size.height/2;
        
        
        //添加拖动手势
        UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(paned:)];
        [self.selectBut addGestureRecognizer:pan];
//        UIPanGestureRecognizer *endPan=[[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(endPaned:)];
//        [self.endBut addGestureRecognizer:endPan];
        
        [self addSubview:self.buttomLabel];
        [self addSubview:self.overLabel];
        [self addSubview:self.endLabel];
        [self addSubview:self.selectBut];
//        [self addSubview:self.endBut];
    }
    return self;
}


- (void) paned:(UIPanGestureRecognizer *) pan{
    if (self.overLabel.frame.size.width/self.frame.size.width >= 1) {
        return;
    }
    
    CGPoint maxPoint = CGPointMake(self.frame.size.width-0.01, self.frame.size.height/2);
    CGPoint minPoint = CGPointMake(0.01, self.frame.size.height/2);
    if (self.selectBut.center.x >= self.frame.size.width) {
        self.selectBut.center = maxPoint;
        return;
        //self.selectBut.center = maxPoint;
    }else if (self.selectBut.center.x <= 0) {
        self.selectBut.center = minPoint;
        return;
    }
    //获取移动的大小
    CGPoint point= [pan translationInView:pan.view];
    NSLog(@"%f",point.x);
    
    //更改视图的中心点坐标
    NSLog(@"%f",self.selectBut.center.x);
    CGPoint points = self.selectBut.center;
    points.x+=point.x;
    self.selectBut.center=points;
    //每次都清空一下消除坐标叠加
    [pan setTranslation:CGPointZero inView:pan.view];
    if (points.x > self.frame.size.width) {
        self.overLabel.frame = CGRectMake(0, 10, self.frame.size.width, self.frame.size.height - 20);
        self.selectBut.center = maxPoint;
    }else {
        self.overLabel.frame = CGRectMake(0, 10, points.x, self.frame.size.height - 20);
    }
    
    if (self.leftPercent) {
        self.leftPercent(self.overLabel.frame.size.width/self.frame.size.width);
    }
    
    
}

//- (void) endPaned:(UIPanGestureRecognizer *) endPan {
//    
//    CGPoint maxPoint = CGPointMake(self.frame.size.width-0.01, self.frame.size.height/2);
//    CGPoint minPoint = CGPointMake(0.01, self.frame.size.height/2);
//    CGPoint limitPoint = CGPointMake(self.selectBut.center.x + 1, self.frame.size.height/2);
////    if (self.endBut.center.x <= self.selectBut.center.x) {
////        self.endBut.center = limitPoint;
////        return;
////    }
////    if (self.endBut.center.x >= self.frame.size.width) {
////        self.endBut.center = maxPoint;
////        return;
////        //self.selectBut.center = maxPoint;
////    }else if (self.endBut.center.x <= 0) {
////        self.endBut.center = minPoint;
////        return;
////    }
//    //获取移动的大小
//    CGPoint point= [endPan translationInView:endPan.view];
//    NSLog(@"%f",point.x);
//    
//    //更改视图的中心点坐标
////    NSLog(@"%f",self.endBut.center.x);
////    CGPoint points = self.endBut.center;
////    points.x+=point.x;
////    self.endBut.center=points;
//    //每次都清空一下消除坐标叠加
//    [endPan setTranslation:CGPointZero inView:endPan.view];
////    self.endLabel.frame = CGRectMake(points.x, self.frame.size.height/2, self.frame.size.width - points.x, 1);
//    if (self.rightPercent) {
//        self.rightPercent(self.endLabel.frame.size.width/self.frame.size.width);
//    }
//}
//

- (void)jumpToTime:(CGFloat)percent {
    self.overLabel.frame = CGRectMake(0, self.frame.size.height/2, self.frame.size.width*percent, 1);
    self.selectBut.center = CGPointMake(self.frame.size.width*percent, self.frame.size.height/2);
}



@end
